<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="如何构建符合 POSIX 实用程序约定的Shell-CLIWhat is ArgtableArgtable是一款开源的ANSI C library，用来解析GNU样式的命令行选项。它通过定义可用于指定命令行语法的声明式API，从而简化了命令行的分析。argtable将自动生成一致的错误处理逻辑和命令行语法的文本描述，这对于一个健壮的cli程序来说，是非常必要的，但是很繁琐。 Introductio">
<meta property="og:type" content="article">
<meta property="og:title" content="如何解析c语言程序的命令行参数？—— Argtable3使用教程">
<meta property="og:url" content="http://example.com/2021/09/23/%E5%A6%82%E4%BD%95%E8%A7%A3%E6%9E%90c%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%EF%BC%9F%E2%80%94%E2%80%94-Argtable3%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="如何构建符合 POSIX 实用程序约定的Shell-CLIWhat is ArgtableArgtable是一款开源的ANSI C library，用来解析GNU样式的命令行选项。它通过定义可用于指定命令行语法的声明式API，从而简化了命令行的分析。argtable将自动生成一致的错误处理逻辑和命令行语法的文本描述，这对于一个健壮的cli程序来说，是非常必要的，但是很繁琐。 Introductio">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/example.png">
<meta property="og:image" content="http://example.com/flowchart.png">
<meta property="article:published_time" content="2021-09-22T16:00:00.000Z">
<meta property="article:modified_time" content="2021-09-22T23:20:52.417Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="c语言">
<meta property="article:tag" content="tool">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/example.png">


<link rel="canonical" href="http://example.com/2021/09/23/%E5%A6%82%E4%BD%95%E8%A7%A3%E6%9E%90c%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%EF%BC%9F%E2%80%94%E2%80%94-Argtable3%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/09/23/%E5%A6%82%E4%BD%95%E8%A7%A3%E6%9E%90c%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%EF%BC%9F%E2%80%94%E2%80%94-Argtable3%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/","path":"2021/09/23/如何解析c语言程序的命令行参数？——-Argtable3使用教程/","title":"如何解析c语言程序的命令行参数？—— Argtable3使用教程"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>如何解析c语言程序的命令行参数？—— Argtable3使用教程 | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E6%9E%84%E5%BB%BA%E7%AC%A6%E5%90%88-POSIX-%E5%AE%9E%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%BA%A6%E5%AE%9A%E7%9A%84Shell-CLI"><span class="nav-number">1.</span> <span class="nav-text">如何构建符合 POSIX 实用程序约定的Shell-CLI</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#What-is-Argtable"><span class="nav-number">1.1.</span> <span class="nav-text">What is Argtable</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Introduction-of-Argtable3"><span class="nav-number">1.2.</span> <span class="nav-text">Introduction of Argtable3</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Quick-Start"><span class="nav-number">1.3.</span> <span class="nav-text">Quick Start</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#demo-code"><span class="nav-number">1.3.1.</span> <span class="nav-text">demo code</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#how-it-works"><span class="nav-number">1.3.2.</span> <span class="nav-text">how it works</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#arg-hdr"><span class="nav-number">1.3.2.1.</span> <span class="nav-text">arg_hdr</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#arg-xxx%E7%BB%93%E6%9E%84%E4%BD%93%E6%97%8F"><span class="nav-number">1.3.2.2.</span> <span class="nav-text">arg_xxx结构体族</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E4%BB%8B%E7%BB%8D-arg-int"><span class="nav-number">1.3.2.3.</span> <span class="nav-text">详细介绍 arg_int</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#The-Argument-Table-%E5%8F%82%E6%95%B0%E8%A1%A8"><span class="nav-number">1.3.2.4.</span> <span class="nav-text">The Argument Table (参数表)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%A7%A3%E6%9E%90%E5%91%BD%E4%BB%A4%E8%A1%8C"><span class="nav-number">1.3.2.5.</span> <span class="nav-text">解析命令行</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4%E5%80%BC"><span class="nav-number">1.3.2.6.</span> <span class="nav-text">设置默认值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86"><span class="nav-number">1.3.2.7.</span> <span class="nav-text">错误处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E7%A4%BA%E9%80%89%E9%A1%B9%E8%AF%AD%E6%B3%95%E5%8F%8A%E5%B8%AE%E5%8A%A9"><span class="nav-number">1.3.2.8.</span> <span class="nav-text">显示选项语法及帮助</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%89%93%E6%89%AB%E5%B9%B2%E5%87%80"><span class="nav-number">1.3.2.9.</span> <span class="nav-text">打扫干净</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%8C%E6%95%B4%E7%9A%84%E4%BD%BF%E7%94%A8Argtable3%E7%9A%84%E6%B5%81%E7%A8%8B%E5%9B%BE"><span class="nav-number">1.3.3.</span> <span class="nav-text">完整的使用Argtable3的流程图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%9B%B8%E5%85%B3%E9%93%BE%E6%8E%A5"><span class="nav-number">1.4.</span> <span class="nav-text">相关链接</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/23/%E5%A6%82%E4%BD%95%E8%A7%A3%E6%9E%90c%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%EF%BC%9F%E2%80%94%E2%80%94-Argtable3%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="如何解析c语言程序的命令行参数？—— Argtable3使用教程 | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          如何解析c语言程序的命令行参数？—— Argtable3使用教程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-09-23 00:00:00 / 修改时间：07:20:52" itemprop="dateCreated datePublished" datetime="2021-09-23T00:00:00+08:00">2021-09-23</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="如何构建符合-POSIX-实用程序约定的Shell-CLI"><a href="#如何构建符合-POSIX-实用程序约定的Shell-CLI" class="headerlink" title="如何构建符合 POSIX 实用程序约定的Shell-CLI"></a>如何构建符合 POSIX 实用程序约定的Shell-CLI</h1><h2 id="What-is-Argtable"><a href="#What-is-Argtable" class="headerlink" title="What is Argtable"></a>What is Argtable</h2><p>Argtable是一款开源的ANSI C library，用来解析GNU样式的命令行选项。它通过定义可用于指定命令行语法的声明式API，从而简化了命令行的分析。<br>argtable将自动生成一致的错误处理逻辑和命令行语法的文本描述，这对于一个健壮的cli程序来说，是非常必要的，但是很繁琐。</p>
<h2 id="Introduction-of-Argtable3"><a href="#Introduction-of-Argtable3" class="headerlink" title="Introduction of Argtable3"></a>Introduction of Argtable3</h2><p>解析程序的命令行一直以来都是一个分散注意力的主要编程任务。Argtable3 库通过允许程序员<strong>将源代码中的命令行选项直接定义为静态的结构数组</strong>来简化作业， 然后**<br>将该数组传递给argtable3库函数，这些函数就会相应地解析命令行。从命令行中提取的值直接保存到用户定义的程序变量中<strong>，主程序就可以访问这些变量。<br>argtable3还可以从同一数组中生成命令行语法的描述，以便显示为联机帮助。根据第三条BSD许可条款，此库是免费的。 注意：</strong>argtable3不向后兼容**。</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="demo-code"><a href="#demo-code" class="headerlink" title="demo code"></a>demo code</h3><ul>
<li><p>入门小例子</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入 argtable3</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;argtable3.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* global arg_xxx structs */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span> *<span class="title">help</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_end</span> *<span class="title">end</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span> &#123;</span><br><span class="line">    <span class="comment">/* the global arg_xxx structs are initialised within the argtable */</span></span><br><span class="line">    <span class="type">void</span> *argtable[] = &#123;</span><br><span class="line">            help = arg_litn(<span class="literal">NULL</span>, <span class="string">&quot;help&quot;</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;display this help and exit&quot;</span>),</span><br><span class="line">            end = arg_end(<span class="number">20</span>)</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="type">int</span> exitcode = <span class="number">0</span>;</span><br><span class="line">    <span class="type">char</span> program[] = <span class="string">&quot;argtable&quot;</span>;</span><br><span class="line">    arg_parse(argc, argv, argtable);</span><br><span class="line">    <span class="comment">/* special case: &#x27;--help&#x27; takes precedence over error reporting */</span></span><br><span class="line">    <span class="keyword">if</span> (help-&gt;count &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Usage: %s&quot;</span>, program);</span><br><span class="line">        arg_print_syntax(<span class="built_in">stdout</span>, argtable, <span class="string">&quot;\n&quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Demonstrate command-line parsing in argtable3.\n\n&quot;</span>);</span><br><span class="line">        arg_print_glossary(<span class="built_in">stdout</span>, argtable, <span class="string">&quot;  %-25s %s\n&quot;</span>);</span><br><span class="line">        exitcode = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">/* deallocate each non-null entry in argtable[] */</span></span><br><span class="line">        arg_freetable(argtable, <span class="keyword">sizeof</span>(argtable) / <span class="keyword">sizeof</span>(argtable[<span class="number">0</span>]));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> exitcode;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>运行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">./argtable --help</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">输出</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Usage: argtable [--<span class="built_in">help</span>]</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Demonstrate command-line parsing <span class="keyword">in</span> argtable3.</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"></span></span><br><span class="line"><span class="language-bash"><span class="comment">#  --help                    display this help and exit</span></span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="how-it-works"><a href="#how-it-works" class="headerlink" title="how it works"></a>how it works</h3><p>Argtable3 提供了一组<strong>arg_xxx结构</strong>，一个用于它支持的每种类型的参数（<strong>文字、整数、双精度、字符串、文件名</strong>等），<br>并且每个结构都能够在命令行上处理该参数的多次出现。此外，<strong>可以为每个选项提供可互换使用的替代短选项 (-x)或长选项 (–xx) 形式</strong>。<br>事实上，每个选项甚至可以选择多个替代的短选项或长选项，或两者兼而有之。</p>
<p>要定义命令行选项，您必须为arg_xxx所需的每种类型的参数（<strong>文字、整数、双精度、字符串、文件名</strong>等）创建一个结构体，并将它们整理到我们称为参数表（<strong>argument table</strong>）的数组中。<br>参数表中结构的顺序定义了命令行选项的预期顺序，不过<strong>解析顺序实际上只对未标记的选项重要</strong>（和python的函数传参规则类似）。<br><strong>参数表本身只是一个void指针数组</strong>（<code>void* arg_table[]</code>），<br>按照惯例，每个arg_xxx结构都有一个已知的<strong>arg_hdr结构体</strong>作为其第一个条目，Argtable3 函数使用它来标识结构体。</p>
<h4 id="arg-hdr"><a href="#arg-hdr" class="headerlink" title="arg_hdr"></a>arg_hdr</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> &#123;</span></span><br><span class="line">    <span class="type">char</span> flag;             <span class="comment">/* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */</span></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span>* shortopts; <span class="comment">/* String defining the short options */</span></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span>* longopts;  <span class="comment">/* String defiing the long options */</span></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span>* datatype;  <span class="comment">/* Description of the argument data type */</span></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span>* glossary;  <span class="comment">/* Description of the option as shown by arg_print_glossary function */</span></span><br><span class="line">    <span class="type">int</span> mincount;          <span class="comment">/* Minimum number of occurences of this option accepted */</span></span><br><span class="line">    <span class="type">int</span> maxcount;          <span class="comment">/* Maximum number of occurences if this option accepted */</span></span><br><span class="line">    <span class="type">void</span>* parent;          <span class="comment">/* Pointer to parent arg_xxx struct */</span></span><br><span class="line">    arg_resetfn* resetfn;  <span class="comment">/* Pointer to parent arg_xxx reset function */</span></span><br><span class="line">    arg_scanfn* scanfn;    <span class="comment">/* Pointer to parent arg_xxx scan function */</span></span><br><span class="line">    arg_checkfn* checkfn;  <span class="comment">/* Pointer to parent arg_xxx check function */</span></span><br><span class="line">    arg_errorfn* errorfn;  <span class="comment">/* Pointer to parent arg_xxx error function */</span></span><br><span class="line">    <span class="type">void</span>* priv;            <span class="comment">/* Pointer to private header data for use by arg_xxx functions */</span></span><br><span class="line">&#125; <span class="type">arg_hdr_t</span>;</span><br></pre></td></tr></table></figure>
<p>最基本的一个类型，所有的其他类型都包含这个类型。一一解释下里面的参数的含义:</p>
<ul>
<li>shortopts: 参数的短名称，比如ls -a 中的a就是短名称;</li>
<li>longopts: 参数中的长名称，比如ls —all 中的all就是长名称;</li>
<li>datatype: 参数的数据类型有, , NULL, ,(时间类型传入format字符串),(rex类型传入匹配串),等;</li>
<li>glossary: 对于参数的描述;</li>
<li>mincount: 参数最少的个数(可以规定是否必须有参数比如设置为0为非必须，1…n为必须);</li>
<li>maxcount: 参数最多的个数，采用Unix风格的，如kernel.exe -l 1 -l 2 -l 3</li>
<li>其他参数非必须。</li>
</ul>
<h4 id="arg-xxx结构体族"><a href="#arg-xxx结构体族" class="headerlink" title="arg_xxx结构体族"></a>arg_xxx结构体族</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 整数</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_int</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">int</span> *ival;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 文字</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 双精度浮点数</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_dbl</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">double</span> *dval;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 字符串</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_str</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> **sval;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_rex</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> **sval;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_file</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> **filename;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> **basename;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> **extension;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *format;</span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">tm</span> *<span class="title">tm_val</span>;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="详细介绍-arg-int"><a href="#详细介绍-arg-int" class="headerlink" title="详细介绍 arg_int"></a>详细介绍 arg_int</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_int</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">arg_hdr</span> <span class="title">hdr</span>;</span></span><br><span class="line">    <span class="type">int</span> count;</span><br><span class="line">    <span class="type">int</span> *ival;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>结构体的第一个数据成员hdr保存了 Argtable3 库函数使用的私有数据。它包含参数的标签字符串等内容（<strong>可以理解为元数据</strong>）。<br>直接公开地访问这些数据是可以的，但很少需要这样做。<br>它的ival成员变量指向持有从命令行提取出的数组，count为数组的长度。ival 数组的存储在arg_int构造时分配。<br><strong>这必须通过arg_int构造函数完成。</strong><br>注意，<strong>你永远不应该自己手动实例化任何arg_xxx结构，而是始终使用提供的构造函数来分配结构，并在完成后使用 arg_freetable 释放它。</strong></p>
<ul>
<li>举例<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_int</span> *<span class="title">s</span>;</span></span><br><span class="line">s = arg_intn(<span class="literal">NULL</span>, <span class="string">&quot;scalar&quot;</span>, <span class="string">&quot;&lt;n&gt;&quot;</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="string">&quot;foo value&quot;</span>);</span><br></pre></td></tr></table></figure>
完成s后将指向一个包含5 个元素数组的内存块的结构体arg_int。<br>甚至，你还可以设置多个别名，短选项被赋予一串单个字符，而长选项被赋予一个逗号分隔的字符串。<br>例如：<code>s = arg_intn(&quot;kKx&quot;, &quot;scalar,foo&quot;, &quot;&lt;n&gt;&quot;, 3, 5, &quot;foo value&quot;);</code></li>
</ul>
<p><img src="/example.png" alt="example.png"><br>如图所示，该<code>s-&gt;hdr</code>结构保留对构造函数的字符串参数的引用等。<br>该<code>s-&gt;count</code>变量初始化为零，因为它表示<code>s-&gt;ival</code>解析命令行后存储在数组中的有效值的数量。<br><code>s-&gt;ival</code>数组的大小由<code>s-&gt;hdr.maxcount</code>给出。</p>
<ul>
<li>实例代码<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入 argtable3</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;argtable3.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* global arg_xxx structs */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_int</span> *<span class="title">int_arrays</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_end</span> *<span class="title">end</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span> &#123;</span><br><span class="line">    <span class="comment">/* the global arg_xxx structs are initialised within the argtable */</span></span><br><span class="line">    <span class="type">void</span> *argtable[] = &#123;</span><br><span class="line">            int_arrays = arg_intn(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;array&quot;</span>, <span class="string">&quot;&lt;number&gt;&quot;</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&quot;please input an integer&quot;</span>),</span><br><span class="line">            end = arg_end(<span class="number">20</span>)</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="type">int</span> exitcode = <span class="number">-1</span>;</span><br><span class="line">    <span class="type">int</span> nerrors = arg_parse(argc, argv, argtable);</span><br><span class="line">    <span class="keyword">if</span> (nerrors == <span class="number">0</span>) &#123;</span><br><span class="line">        exitcode = <span class="number">0</span>;</span><br><span class="line">        <span class="type">int</span> *a = int_arrays-&gt;ival;</span><br><span class="line">        <span class="type">int</span> len = int_arrays-&gt;count;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;input numbers are as following:\n&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; ++i) &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d  &quot;</span>, a[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> exitcode;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">运行</span></span><br><span class="line">./argtable -a 1 -a 2 --array 3</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">输出</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">input numbers are as following:</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1  2  3</span>  </span><br></pre></td></tr></table></figure></li>
</ul>
<p>其他的参数结构体使用方法大同小异。</p>
<h4 id="The-Argument-Table-参数表"><a href="#The-Argument-Table-参数表" class="headerlink" title="The Argument Table (参数表)"></a>The Argument Table (参数表)</h4><p>在构建完各种类型参数的结构体之后，我们需要统一将它们组装到一个结构体数组里面，类型为<code>void **</code>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span> *<span class="title">a</span> =</span> arg_litn(<span class="string">&quot;a&quot;</span>, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;the -a option&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span> *<span class="title">b</span> =</span> arg_litn(<span class="string">&quot;b&quot;</span>, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;the -b option&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span> *<span class="title">c</span> =</span> arg_litn(<span class="string">&quot;c&quot;</span>, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;the -c option&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_int</span> *<span class="title">scal</span> =</span> arg_intn(<span class="literal">NULL</span>, <span class="string">&quot;scalar&quot;</span>, <span class="string">&quot;&lt;n&gt;&quot;</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;foo value&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_lit</span> *<span class="title">verb</span> =</span> arg_litn(<span class="string">&quot;v&quot;</span>, <span class="string">&quot;verbose&quot;</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;verbose output&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_file</span> *<span class="title">o</span> =</span> arg_filen(<span class="string">&quot;o&quot;</span>, <span class="literal">NULL</span>,<span class="string">&quot;myfile&quot;</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="string">&quot;output file&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_file</span> *<span class="title">file</span> =</span> arg_filen(<span class="literal">NULL</span>, <span class="literal">NULL</span>, <span class="string">&quot;&lt;file&gt;&quot;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="string">&quot;input files&quot;</span>);</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">arg_end</span> *<span class="title">end</span> =</span> arg_end(<span class="number">20</span>);</span><br><span class="line"><span class="comment">// 这就是我们的参数列表</span></span><br><span class="line"><span class="type">void</span> *argtable[] = &#123;a, b, c, scal, verb, o, file, end&#125;; </span><br></pre></td></tr></table></figure>

<p>其中，<code>arg_end</code> 结构体是一个特殊的结构体，因为它不代表任何命令行选项。<br>它主要标记 argtable 数组的结尾，同时它也存储在处理命令行参数时遇到的任何解析器错误。<br>传递给arg_end构造函数的整数参数是它将存储的最大错误数，任何更多的错误都将被丢弃并替换为单个错误消息“错误太多”。<br><strong><code>arg_end</code>必须要有。</strong></p>
<p>你还可以使用<code>arg_nullcheck</code>检查参数表中是否有空指针，（感觉这个功能很鸡肋）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (arg_nullcheck(argtable) != <span class="number">0</span>)</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;error: insufficient memory\n&quot;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="解析命令行"><a href="#解析命令行" class="headerlink" title="解析命令行"></a>解析命令行</h4><p>我们使用<code>arg_parse</code>函数来做到这一点，它返回它遇到的解析错误的数量。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> nerrors = arg_parse(argc, argv, argtable);</span><br></pre></td></tr></table></figure>
<p>如果没有错误的话（<code>nerrors == 0</code>），那么我们已经成功解析了命令行,你想要在命令行中提取的数据也都存在了参数表这个<code>void**</code>里面，正常地访问它即可。</p>
<h4 id="设置默认值"><a href="#设置默认值" class="headerlink" title="设置默认值"></a>设置默认值</h4><p>对此的解决方法很简单，直接在调用<code>arg_parse</code>之前设置<code>arg_table</code>的值，在命令行中有用户输入的值才会被覆盖。<br>其余的，就是你一开始设置的默认值。</p>
<h4 id="错误处理"><a href="#错误处理" class="headerlink" title="错误处理"></a>错误处理</h4><p>如果 arg_parse 函数报告错误，那么我们需要显示它们，因为 arg_parse 本身不会这样做。<br>使用<code>arg_print_errors</code>即可，arg_end的内部结构无需关心。<br>函数定义</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">arg_print_errors</span><span class="params">(FILE* fp, <span class="keyword">struct</span> arg_end* end, <span class="type">const</span> <span class="type">char</span>* progname)</span>;</span><br></pre></td></tr></table></figure>
<p>使用实例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">If (nerrors &gt; <span class="number">0</span>)</span><br><span class="line">    arg_print_errors(<span class="built_in">stdout</span>, end, <span class="string">&quot;myprog&quot;</span>); <span class="comment">// stdout 文件描述符</span></span><br></pre></td></tr></table></figure>
<p>arg_parse函数不打印错误消息是因为它可以被多次调用以使用替代参数表解析命令行，而不会过早显示无关的错误消息。<br>因此，我们可以为那些具有互斥命令行选项集的程序定义单独的参数表，依次尝试每个参数表，直到找到一个成功的候选者。<br>如果所有参数表都不能满足，那么我们可以选择打印所有参数表的错误消息，或者可能只显示与最匹配的错误信息。无论如何，我们可以控制显示哪些消息。</p>
<h4 id="显示选项语法及帮助"><a href="#显示选项语法及帮助" class="headerlink" title="显示选项语法及帮助"></a>显示选项语法及帮助</h4><p>如果您希望您的程序显示在线帮助，您可以使用<code>arg_print_syntax</code>函数来显示从参数表派生的命令行语法。<br>该函数实际上有两种形式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">arg_print_syntax</span><span class="params">(FILE *fp, <span class="type">void</span> **argtable, <span class="type">const</span> <span class="type">char</span> *suffix)</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">arg_print_syntaxv</span><span class="params">(FILE *fp, <span class="type">void</span> **argtable, <span class="type">const</span> <span class="type">char</span> *suffix)</span>;</span><br></pre></td></tr></table></figure>
<p>你可以使用<code>void arg_print_glossary(FILE *fp, void **argtable, const char *format);</code>来输出提示。</p>
<p>在文章的开始的示例中已经展示过这两个函数的使用方法了，这里不再赘述。</p>
<h4 id="打扫干净"><a href="#打扫干净" class="headerlink" title="打扫干净"></a>打扫干净</h4><p>在我们的程序结束时，我们需要释放分配给每个arg_xxx结构的内存。我们可以通过分别调用<code>arg_freetable</code>来做到这一点，<br>但有函数可以更方便地为我们做到这一点。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arg_freetable(argtable, <span class="keyword">sizeof</span>(argtable) / <span class="keyword">sizeof</span>(argtable[<span class="number">0</span>]));</span><br></pre></td></tr></table></figure>
<p>它将遍历一个参数表并代表我们调用它的每个元素。<br>请注意，第二个参数<code>sizeof(argtable)/sizeof(argtable[0])</code>仅表示 argtable 数组中的元素数。（使用了内存对齐？）<br>完成此功能后，所有 argtable 数组条目都将设置为NULL。</p>
<h3 id="完整的使用Argtable3的流程图"><a href="#完整的使用Argtable3的流程图" class="headerlink" title="完整的使用Argtable3的流程图"></a>完整的使用Argtable3的流程图</h3><p><img src="/flowchart.png" alt="流程图"></p>
<h2 id="相关链接"><a href="#相关链接" class="headerlink" title="相关链接"></a>相关链接</h2><p><a target="_blank" rel="noopener" href="https://www.argtable.org/tutorial/">Argtable3教程介绍</a><br><a target="_blank" rel="noopener" href="https://github.com/argtable/argtable3/releases/">github地址</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80/" rel="tag"># c语言</a>
              <a href="/tags/tool/" rel="tag"># tool</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/16/Redis%E5%9C%A8Java%20%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BD%BF%E7%94%A8%20%E2%80%94%E2%80%94%20%E4%BD%BF%E7%94%A8%20Jedis%20%E6%B5%8B%E8%AF%95%20redis%E6%93%8D%E7%BA%B5%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%91%BD%E4%BB%A4/" rel="prev" title="Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令">
                  <i class="fa fa-chevron-left"></i> Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/25/%E5%8F%8D%E5%B0%84%E7%9A%84%E5%9F%BA%E7%A1%80%E3%80%81Class%E3%80%81Field%E3%80%81Method%E3%80%81Constructor%E7%B1%BB/" rel="next" title="反射的基础、Class、Field、Method、Constructor类">
                  反射的基础、Class、Field、Method、Constructor类 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
